﻿using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class BTNodePopupWindow : EditorWindow
{
    #region Variable
    static BTNodePopupWindow curPopup;
    string wantedName = "Enter a name...";
    ///是否是创建tree
    bool isCreateTree = true;
    #endregion

    #region Main Mehtods
    public static void InitNodePopup()
    {
        curPopup = EditorWindow.GetWindow<BTNodePopupWindow>();
        curPopup.titleContent = new GUIContent("Node Popup");
       
    }

     public static void InitNodePopup(bool isCreateTree)
    {
        curPopup = EditorWindow.GetWindow<BTNodePopupWindow>();
        curPopup.isCreateTree = isCreateTree;
        curPopup.titleContent = new GUIContent("Node Popup");
       
    }

    void OnGUI()
    {
        if(isCreateTree)
        {
            this.drawCreateTree();
        }
        else
        {
            this.drawCreateNode();
        }
       
    }
    #endregion

    #region Utility Methods
    private void drawCreateTree()
    {
        GUILayout.Space(20);
        GUILayout.BeginHorizontal();
        GUILayout.Space(20);

        GUILayout.BeginVertical();

        EditorGUILayout.LabelField("Create New Node:", EditorStyles.boldLabel);

        wantedName = EditorGUILayout.TextField("Enter Name:",wantedName);
        string des = EditorGUILayout.TextField("Enter Description:", "Tree Description");
        GUILayout.Space(20);

        GUILayout.BeginHorizontal();
        if(GUILayout.Button("Create Tree", GUILayout.Height(40)))
        {
            if(!string.IsNullOrEmpty(wantedName) && wantedName!= "Enter a name...")
            {
                BTNodeUtils.CreateNewGraph(wantedName, des);
                curPopup.Close();
            }
            else
            {
                EditorUtility.DisplayDialog("Note Message:", "Please Enter a Tree Name", "OK");
            }
        }
        if(GUILayout.Button("Cancel",GUILayout.Height(40)))
        {
            curPopup.Close();
        }
        GUILayout.EndHorizontal();

        GUILayout.EndVertical();

        GUILayout.Space(20);
        GUILayout.EndHorizontal();
        GUILayout.Space(20);
    }

private void drawCreateNode()
    {
        GUILayout.Space(20);
        GUILayout.BeginHorizontal();
        GUILayout.Space(20);

        GUILayout.BeginVertical();

        EditorGUILayout.LabelField("Create New Tree:", EditorStyles.boldLabel);

        wantedName = EditorGUILayout.TextField("Enter Name:",wantedName);
        string des = EditorGUILayout.TextField("Enter Description:", "Tree Description");
        GUILayout.Space(20);

        GUILayout.BeginHorizontal();
        if(GUILayout.Button("Create Tree", GUILayout.Height(40)))
        {
            if(!string.IsNullOrEmpty(wantedName) && wantedName!= "Enter a name...")
            {
                BTNodeUtils.CreateNewGraph(wantedName, des);
                curPopup.Close();
            }
            else
            {
                EditorUtility.DisplayDialog("Note Message:", "Please Enter a Tree Name", "OK");
            }
        }
        if(GUILayout.Button("Cancel",GUILayout.Height(40)))
        {
            curPopup.Close();
        }
        GUILayout.EndHorizontal();

        GUILayout.EndVertical();

        GUILayout.Space(20);
        GUILayout.EndHorizontal();
        GUILayout.Space(20);
    }
    #endregion

}
